//
//  LJP_SecondViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/10/4.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_SecondViewController.h"
#import <AFNetworking.h>
#import "GuideModel.h"
#import "LJP_FirstListTableViewCell.h"
#import "LJP_ThirdViewController.h"
@interface LJP_SecondViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *plistArray;

@end

@implementation LJP_SecondViewController
- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.plistArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_FirstListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qwer"];
    if (cell == nil) {
        cell = [[LJP_FirstListTableViewCell alloc] init];
    }
    cell.model = self.plistArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_ThirdViewController *third = [[LJP_ThirdViewController alloc] init];
    third.albumId = [[self.plistArray[indexPath.row] albumId] stringValue];
    third.tName = [self.plistArray[indexPath.row] title];
    [self.navigationController pushViewController:third animated:YES];
    
}

- (void)dataHandle
{
    self.plistArray = [NSMutableArray array];
    NSString *string = [self.tname stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=22&device=android&pageId=1&pageSize=20&status=0&tagName=%@", string];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *array = [responseObject objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            GuideModel *model = [[GuideModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.plistArray addObject:model];
            [self.tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.tname;
    [self dataHandle];
    [self tableViewSubview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
